home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 3 / Cream of the Crop 3.iso / comm / wnos5src.zip / AX25DUMP.C < prev    next >
Text File  |  1993-08-09  |  5KB  |  219 lines

  1. /* DAMA enhancements by DL1BKE, 920531 */
  2.  
  3. #include <stdio.h>
  4. #include "global.h"
  5. #include "config.h"
  6. #ifdef AX25
  7. #include "mbuf.h"
  8. #include "ax25.h"
  9. #include "timer.h"
  10. #include "trace.h"
  11. #include "socket.h"
  12.  
  13. static char * near
  14. decode_type(int type)
  15. {
  16.     switch(type){
  17.         case I:
  18.             return "I";
  19.         case SABM:
  20.             return "SABM";
  21.         case DISC:
  22.             return "DISC";
  23.         case DM:
  24.             return "DM";
  25.         case UA:
  26.             return "UA";
  27.         case RR:
  28.             return "RR";
  29.         case RNR:
  30.             return "RNR";
  31.         case REJ:
  32.             return "REJ";
  33.         case FRMR:
  34.             return "FRMR";
  35.         case UI:
  36.             return "UI";
  37.         default:
  38.             return "[invalid]";
  39.     }
  40. }
  41.  
  42. /* Figure out the frame type from the control field
  43.  * This is done by masking out any sequence numbers and the
  44.  * poll/final bit after determining the general class (I/S/U) of the frame
  45.  */
  46. static int near
  47. ftype(int control)
  48. {
  49.     if((control & 1) == 0)  /* An I-frame is an I-frame... */
  50.         return I;
  51.     if(control & 2)         /* U-frames use all except P/F bit for type */
  52.         return (control & ~PF);
  53.     else                    /* S-frames use low order 4 bits for type */
  54.         return (control & 0x0f);
  55. }
  56.  
  57. /* Dump an AX.25 packet header */
  58. void
  59. ax25_dump(FILE *fp,struct mbuf **bpp,int check)
  60. {
  61.     char tmp[AXBUF], tmp1[AXBUF], frmr[3], cp[2 * AXALEN];
  62.     int control, pid, seg, ipcam, cmdrsp, type, unsegmented = 0;
  63.     struct mbuf *bp;
  64.  
  65.     trprintf(fp,"AX25: ");
  66.  
  67.     if(pullup(bpp,cp,2 * AXALEN) == (2 * AXALEN)) {
  68. #ifdef DAMA_S
  69.         char dama_master = DAMA - (cp[ALEN + AXALEN] & DAMA);  /* DAMA bke 920609 */
  70.  
  71.         trprintf(fp,"%s %sto %s ",
  72.             pax25(tmp,&cp[AXALEN]),
  73.             dama_master ? "[DAMA] " : "",
  74.             pax25(tmp1,cp));
  75. #else
  76.         trprintf(fp,"%s to %s ",
  77.             pax25(tmp,&cp[AXALEN]),
  78.             pax25(tmp1,cp));
  79. #endif
  80.         if(((cp[ALEN]) & C) == (cp[ALEN + AXALEN] & C)) {
  81.             cmdrsp = VERS1;
  82.         } else {
  83.             cmdrsp = (cp[ALEN] & C) ? DST_C : SRC_C;
  84.         }
  85.         if((cp[ALEN + AXALEN] & E) == 0) {
  86.             trprintf(fp,"via ");
  87.             while((cp[ALEN] & E) == 0) {
  88.                 if(pullup(bpp,cp,AXALEN) == AXALEN)
  89.                     trprintf(fp,"%s%s ",pax25(tmp,cp),(cp[ALEN] & REPEATED) ? "*" : "");
  90.                 else
  91.                     goto err;
  92.             }
  93.         }
  94.     } else {
  95.         goto err;
  96.     }
  97.  
  98.     if((control = PULLCHAR(bpp)) == -1) {
  99. err:
  100.         trprintf(fp,"bad header\n");
  101.         return;
  102.     }
  103.     trprintf(fp,"%s",decode_type((type = ftype(control))));
  104.  
  105.     if(control & PF) {
  106.         switch(cmdrsp) {
  107.         case CMD:
  108.             trprintf(fp,"(P)");
  109.             break;
  110.         case RESP:
  111.             trprintf(fp,"(F)");
  112.             break;
  113.         default:
  114.             trprintf(fp,"(P/F)");
  115.             break;
  116.         }
  117.     }
  118.  
  119.     /* Dump sequence numbers */
  120.     if((type & 0x3) != U)               /* I or S frame? */
  121.         trprintf(fp," NR=%d",(control>>5)&7);
  122.     if(type == I || type == UI) {
  123.         if(type == I) {
  124.             trprintf(fp," NS=%d",(control>>1)&7);
  125.         }
  126.         /* Decode I field */
  127.         if((pid = PULLCHAR(bpp)) != -1){        /* Get pid */
  128.             if(pid == PID_SEGMENT){
  129.                 seg = PULLCHAR(bpp);
  130.                 trprintf(fp,"%s remain %u",seg & SEG_FIRST ?
  131.                     " First seg;" : "",seg & SEG_REM);
  132.                 if(seg & SEG_FIRST)
  133.                     pid = PULLCHAR(bpp);
  134.             } else
  135.                 unsegmented = 1;
  136.  
  137. /* IPCAM-feature - DB3FL.910104 */
  138.             bp = *bpp;
  139.             if(pid == PID_NO_L3 && bp->cnt >= 20
  140.               && bp->data[0] == 0x45
  141.               && bp->data[1] == 0x00
  142.               && bp->data[2] < 0x02) {
  143.                 pid = PID_IP;
  144.                 ipcam = 1;
  145.             }
  146.             else
  147.                 ipcam=0;
  148. /* */
  149.             if(pid == PID_SEGMENT) {
  150.                 trprintf(fp,"\n");
  151.             } else {
  152.                 trprintf(fp," pid=");
  153.                 switch(pid){
  154.                     case PID_ARP:
  155.                         trprintf(fp,"ARP\n");
  156.                         arp_dump(fp,bpp);
  157.                         break;
  158.                     case PID_NETROM:
  159.                         trprintf(fp,"NET/ROM\n");
  160. #ifdef NETROM
  161.                         /* Don't verify checksums unless unsegmented */
  162.                         netrom_dump(fp,bpp,unsegmented);
  163. #endif
  164.                         break;
  165.                     case PID_IP:
  166.                         if(ipcam)
  167.                             trprintf(fp,"IPCAM\n");
  168.                         else
  169.                             trprintf(fp,"IP\n");
  170.                         /* Don't verify checksums unless unsegmented */
  171.                         ip_dump(fp,bpp,unsegmented);
  172.                         break;
  173.                     case PID_X25:
  174.                         trprintf(fp,"X.25\n");
  175.                         break;
  176.                     case PID_TEXNET:
  177.                         trprintf(fp,"TEXNET\n");
  178.                         break;
  179.                     case PID_FLEXNET:
  180.                         trprintf(fp,"FLEXNET\n");
  181. #ifdef FLEXNET
  182.                         flexnet_dump(fp,bpp);        /* to be written */
  183. #endif
  184.                         break;
  185.                     case PID_NO_L3:
  186.                         trprintf(fp,"Text\n");
  187.                         break;
  188.                     default:
  189.                         trprintf(fp,"0x%x\n",pid);
  190.                 }
  191.             }
  192.         }
  193.     } else if(type == FRMR && pullup(bpp,frmr,3) == 3){
  194.         trprintf(fp," Vr=%d Vs=%d",
  195.             (frmr[1] >> 5) & MMASK,(frmr[1] >> 1) & MMASK);
  196.         if(frmr[2] & W)
  197.             trprintf(fp," Invalid control field");
  198.         if(frmr[2] & X)
  199.             trprintf(fp," Illegal I-field");
  200.         if(frmr[2] & Y)
  201.             trprintf(fp," Too-long I-field");
  202.         if(frmr[2] & Z)
  203.             trprintf(fp," Invalid seq number");
  204.         trprintf(fp,": %s\n",decode_type(ftype(frmr[0])));
  205.     } else {
  206.         trprintf(fp,"\n");
  207.     }
  208. }
  209.  
  210. /* Return 1 if this packet is directed to us, 0 otherwise. Note that
  211.  * this checks only the ultimate destination, not the digipeater field
  212.  */
  213. int
  214. ax_forus(struct iface *iface,struct mbuf *bp)
  215. {
  216.     return (addreq(bp->data,iface->hwaddr));
  217. }
  218.  
  219. #endif /* AX25 */